class EditionDatesController < ApplicationController

  #
  #
  def create
    @edition = Edition.find(params[:new_date][:id])
	new_edition_date = EditionDate.new(:country_name => params[:new_date][:country_name])
	new_edition_date.edition_id = @edition.id
	
	
	new_date = FullDate.find(:first, :conditions => ["day = ? AND month = ? AND year = ?",
                                    	params[:new_date][:day], params[:new_date][:month], params[:new_date][:year]])
										
	#
	if new_date.nil?
	  new_edition_date.build_full_date(:day => params[:new_date][:day],
	                                  :month => params[:new_date][:month],
									  :year => params[:new_date][:year])
	else
	  new_edition_date.full_date = new_date
	end
	
	#
	respond_to do |format|
	  if new_edition_date.save
	    format.html {redirect_to_request_uri}
		format.js {
		  render :update do |page|
		    page.replace_html("edition-dates", :partial => "dates/dates",
			                           :locals => {:dates => @edition.edition_dates,
												   :item => @edition,
												   :dates_url => edition_dates_url,
												   :events => nil } )
		  end
		}
	  else
	    format.html {redirect_to_request_uri}
		format.js {
		  render :update do |page|
		    page.replace_html(:errors, artist_date)
		  end
		}
	  end
	end
	
  end
  
  #
  #
  def update
    edition_date = EditionDate.find(params[:id])
	@edition = edition_date.edition
	new_day = params[:edit_date][:day]
	new_month = params[:edit_date][:month]
	new_year = params[:edit_date][:year]
	
	if edition_date.day != new_day || edition_date.month != new_month || edition_date_year != new_year
	  new_date = FullDate.find(:first, :conditions => ["day = ? AND month = ? AND year = ?",
                                                      new_day, new_month, new_year])
	  
	  #
	  if new_date.nil?
	    edition_date.build_full_date(:day => new_day,
	                                :month => new_month,
			  						:year => new_year)
	  else
	    edition_date.full_date = new_date
	  end
	end
	
	#if params[:edit_date][:country_name] != edition_date
	
	#
	respond_to do |format|
	  if edition_date.update_attributes(:country_name => params[:edit_date][:country_name])
	    format.html {redirect_to_request_uri}
		format.js {
		  render :update do |page|
		    page.replace_html("edition-dates", :partial => "dates/dates",
						                      :locals => { :dates => @edition.edition_dates,
												           :item => @edition,
													       :dates_url => edition_dates_url,
													       :events => nil }  )
		  end
		}
	  else
	    format.html {redirect_to_request_uri}
		format.js {
		  render :update do |page|
		    page.replace_html(:errors, artist_date)
		  end
		}
	  end
	end
  end
  
  #
  #
  def destroy
    bad_date = EditionDate.find(params[:id])
	@edition = Edition.find(bad_date.edition_id)
	
	bad_date.delete
	
	#entry = Hash.new
	#entry.store("deleted date", [@artist.name])
	#Edit.create_edit(entry, @artist)
	
	respond_to do |format|
	  format.html {redirect_to_request_uri}
	  format.js {
	    render :update do |page|
	      page.replace_html("edition-dates", :partial => "dates/dates",
		                             :locals => {:dates => @edition.edition_dates,
												 :item => @edition,
												 :dates_url => edition_dates_url,
												 :events => nil } )
		  end
	  }
	end
  end

end
